﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace QuickResizer
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static int Main(string[] args)
		{
			// if args are passed, we are in batch mode.  Resize pictures
			// "0" "C:\Users\Russell\Desktop\Sandbox Pictures\sandbox\WP_000013.jpg"
			if (args.Length > 0)
			{
				// change the cursor to an hourglass
				Cursor storedCursor = Cursor.Current;
				Cursor.Current = Cursors.WaitCursor;

				try
				{
					if (Uninstall(args[0]))
					{
						// revert cursor back to normal
						Cursor.Current = storedCursor;
						return 0;
					}

					if (SetKeys(args[0]))
					{
						// revert cursor back to normal
						Cursor.Current = storedCursor;
						return 0;
					}
				}
				catch (Exception ex)
				{
					EventLog.WriteEntry("QuickResizer", ex.Message + " " + ex.StackTrace);
					return 1;
				}

				// resizerIndex tells us which resizer size was chosen
				string resizerIndexParam = args[0];
				int resizerIndex;

				if (int.TryParse(resizerIndexParam, out resizerIndex))
				{
					Resizers resizers = new Resizers();
					resizers.LoadFromConfiguration();

					string file;
					// loop thru the files, resizing them
					for (int i = 1; i < args.Length; i++)
					{
						file = args[i];
						ImageHelper.Resize(file, resizers[resizerIndex]);
					}
				}
				else
				{
					MessageBox.Show("Invalid Resizer Type.");
				}
				// revert cursor back to normal
				Cursor.Current = storedCursor;
			}
			else
			{	// fire up the configuration screen
				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);
				Application.Run(new Configuration());
			}

			return 0;
		}

		private static bool SetKeys(string arg)
		{
			if (arg.ToLower().Equals("/s"))
			{
				Resizers resizers = new Resizers();
				resizers.LoadFromConfiguration();
				foreach (ResizerItem item in resizers.Values)
				{
					RegistryHelper.SetRegistryKeys(item);
				}
				return true;
			}
			return false;
		}

		private static bool Uninstall(string arg)
		{
			string[] argList = arg.Split('=');
			if (argList[0].ToLower().Equals("/u"))
			{
				if (argList.Length > 1 && string.IsNullOrWhiteSpace(argList[1]) == false)
				{
					string productCode = argList[1];
					UninstallRegistryKeys();
					string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
					ProcessStartInfo uninstallProcess = new ProcessStartInfo(path + "\\msiexec.exe", "/x " + productCode);
					Process.Start(uninstallProcess);
				}
				return true;
			}
			return false;
		}

		private static void UninstallRegistryKeys()
		{
			Resizers resizers = new Resizers();
			resizers.LoadFromConfiguration();
			foreach (ResizerItem item in resizers.Values)
			{
				RegistryHelper.RemoveRegistryKeys(item);
			}
		}
	}
}
